package gui;

import java.io.*;
public class doRLE_decompress {
      
      final static boolean DisplaySize  = true;
   
      FileOutputStream OutputStream = null;
      FileInputStream InputStream = null;
      byte[] CharHolder = new byte[1];
   
      void putCharacterInOutputStream(int i) throws IOException {
         OutputStream.write((byte)i);
      }
   
   
      int getNextCharacter() throws IOException {
         int ReadByte;
      
         if ((ReadByte = InputStream.read(CharHolder, 0, 1)) == -1) {
            return -1;
         }
      
         return CharHolder[0];
      }
   
   
		public void doDecompression(String input_file, String output_file) throws IOException {
         long start_time;
      
         
      
         try {
            InputStream = new FileInputStream(input_file);
            OutputStream = new FileOutputStream(output_file);
         
            int LastCharacter = -1; 
            int SecondLastCharacter = -2; 
         
            int TempChar;
            int RepeatCount;
            while ((TempChar = getNextCharacter()) >= 0)  {
               putCharacterInOutputStream(TempChar);
               if (SecondLastCharacter == LastCharacter) {
                  if (TempChar == LastCharacter) {
                     RepeatCount = getNextCharacter();
                     if (RepeatCount < 0) {
                        break; 
                     }
                  
                     while ((TempChar = getNextCharacter()) == LastCharacter) {
                        RepeatCount += 256;
                       }
                 
                     if (TempChar < 0) {
               
                        RepeatCount += (RepeatCount % 256) == 0 ? 256: 0;
                     }
                  
                     while (RepeatCount-- > 0) {
                       
                        putCharacterInOutputStream(LastCharacter);
                     }
                  
                     if (TempChar < 0) {
                        break; 	                }
                     
                     putCharacterInOutputStream(TempChar);
                  }
               }
               
               SecondLastCharacter = LastCharacter;
               LastCharacter = TempChar;
            }
         
            InputStream.close();
            OutputStream.close();
         }
            catch(Exception e) {
               printf("\nError while processing file:"+e.getMessage());
         
            }
      
         
      
         if (DisplaySize) {
            File inFile = new File(input_file);
            printf("Original file size:   " + inFile.length() + " bytes.");
            File outFile = new File(output_file);
            printf("Compressed file size: " + outFile.length() + " bytes.");
         }
      }
   
   
      static void printf(String s) {
         System.out.println(s);
      }
   
   
   
      public static void main(String[] args) throws IOException {
         doRLE_decompress RLE = new doRLE_decompress();
      
         String input_file = null;
         String output_file = null;
      
         if (args.length > 0) {
            input_file = args[0];
            printf("Compressed file: " + args[0]);
         } 
         else {
            printf("Error: No input file specified");
            System.exit(0);
         }
      
         if (args.length > 1) {
            output_file = args[1];
            printf("Decompressed file: " + args[1]);
         } 
         else {
            printf("Error: No output file specified");
            System.exit(0);
         }
      
         RLE.doDecompression(input_file, output_file);
      }
   
   }